<?php

include 'confidential_settings.php';
// This include contains 5 variables:
//  - db_name: database name
//  - table_name_x: table name
//  - username: username
//  - password: password
//  - localhost: host name

function get_corr_value_day($year,$month,$day){
	include 'gsc_corrections.php';

	$ret_corr_value = 0;
	for($i=0; $i< count($corr_year); $i++){
		if((trim($corr_year[$i]) == trim($year))&&(trim($corr_month[$i]) == trim($month))&&(trim($corr_day[$i]) == trim($day))){
			$ret_corr_value = $corr_value[$i];
			return $ret_corr_value;
		}
	}
	return $ret_corr_value;
}


$dbh=mysql_connect ($localhost, $username, $password) or die('Cannot connect to the database because: ' . mysql_error());

mysql_select_db ($db_name) or die(mysql_error()); 

$result = mysql_query("SELECT YEAR(date_entered), DAY(date_entered), MONTH(date_entered), SUM(val) FROM $table_name_gsc WHERE DATE_SUB(CURDATE(),INTERVAL 25 DAY) <= DATE(date_entered) GROUP BY YEAR(date_entered), MONTH(date_entered), DAY(date_entered)");

mysql_close($dbh);


mysql_data_seek ($result , 0);

$max = 0;
$nb = 0;
while($row = mysql_fetch_array($result)){
	$corr_value_tmp = get_corr_value_day($row['YEAR(date_entered)'],$row['MONTH(date_entered)'],$row['DAY(date_entered)']);
	$tmp = $row['SUM(val)'];
	$tmp = $tmp + (1000*$corr_value_tmp);
	if($tmp > $max){
		$max = $tmp;
	}
	$nb = $nb + 1;
}
$max = $max * 1.05;

 
mysql_data_seek ($result , 0);

echo '<img src="http://chart.apis.google.com/chart?cht=bvs&chd=t:';
if($row = mysql_fetch_array($result)){
	$corr_value_tmp = get_corr_value_day($row['YEAR(date_entered)'],$row['MONTH(date_entered)'],$row['DAY(date_entered)']);
	$tmp = $row['SUM(val)'];
	$tmp = $tmp + (1000*$corr_value_tmp);
	$tmp = $tmp*100/$max;
	echo $tmp;
}
while($row = mysql_fetch_array($result)){
	$corr_value_tmp = get_corr_value_day($row['YEAR(date_entered)'],$row['MONTH(date_entered)'],$row['DAY(date_entered)']);
	$tmp = $row['SUM(val)'];
	$tmp = $tmp + (1000*$corr_value_tmp);
	$tmp = $tmp*100/$max;
	echo ',';
	echo $tmp;
}
echo '&chs=750x400&chg=20,20&chxt=x,y&chxr=1,0,';
echo $nb;
echo '|1,0,';
echo $max;


mysql_data_seek ($result , 0);

echo '&chxl=0:|';
while($row = mysql_fetch_array($result)){
	echo $row['DAY(date_entered)'];
	echo '/';
	echo $row['MONTH(date_entered)'];
	echo '|';
}

echo '" />';

?>
